# Flores, Alejandro and Alexander Coppock
# Do Bilinguals Respond More Favorably to Candidate Advertisements in English or in Spanish?
# Forthcoming, Political Communication

rm(list = ls())

# Set your working directory to the replication archive directory

# Install software if necessary
# install.packages(c("ggplot2", "scales"))

library(ggplot2)
library(scales)

ate_bilingual <- 0.05
ate_monolingual <- -0.15

prop_bilingual <- seq(0, 1, by = .01)
prop_mistargeted <- seq(0, 1, by = .01)

combinations_df <-
  expand.grid(prop_bilingual = prop_bilingual, prop_mistargeted = prop_mistargeted)
ates <- rep(NA, nrow(combinations_df))

for (i in 1:nrow(combinations_df)) {
  p_b <- combinations_df[i, "prop_bilingual"]
  p_m <- combinations_df[i, "prop_mistargeted"]
  
  ates[i] <- p_b * ate_bilingual + (1 - p_b) * p_m * ate_monolingual
  
}

combinations_df$`Net Effect of Spanish-Language Ads` <- ates

g <- 
ggplot(combinations_df, aes(prop_bilingual, prop_mistargeted)) +
  geom_tile(aes(fill = `Net Effect of Spanish-Language Ads`)) +
  scale_fill_gradient2(low = muted("red"), mid = "white",
                       high = muted("blue"), midpoint = 0, space = "Lab") +
  #scale_color_gradient2(low = "red", high = "blue", mid = "white", midpoint = 0) +
  stat_contour(aes(z = `Net Effect of Spanish-Language Ads`),binwidth = 0.02) +
  theme_light() +
  xlab("Proportion of Bilingual Voters in Electorate \n Contour Lines Spaced 2 Percentage Points Apart") + 
  ylab("Proportion of Monolingual Voters Mistargeted") + 
  theme(legend.position = "bottom", legend.key.width = unit(3, "lines"))

g


